import Vue from 'vue'
import Router from 'vue-router'
import HelloWorld from '@/components/HelloWorld'
import Homepage from '@/components/Homepage'
import Classify from '@/components/Classify'
import Shopping from '@/components/Shopping'
import Mine from '@/components/Mine'
import Detail from '@/components/Detail'
import Login from '@/components/Login'
import Register from '@/components/Register'
import Code from '@/components/Code'
import Edit from '@/components/Edit'
import Swipdetail from '@/components/Swipdetail'


Vue.use(Router)

var router =  new Router({
  routes: [
    {
      path: '/',
      name: 'Homepage',
      component: Homepage
    },
    {
      path: '/Classify',
      name: 'Classify',
      component: Classify
    },
    {
      path: '/Shopping',
      name: 'Shopping',
      component: Shopping
    },
    {
      path: '/Mine',
      name: 'Mine',
      component: Mine
    },
    {
      path: '/Detail',
      name: 'Detail',
      component: Detail
    },
    {
      path: '/register',
      name: 'Register',
      component: Register
    },
    {
      path: '/login',
      name: 'Login',
      component: Login
    },
    {
      path: '/code',
      name: 'Code',
      component: Code
    },
    {
      path: '/edit',
      name: 'Edit',
      component: Edit
    },
    {
      path: '/swipdetail',
      name: 'Swipdetail',
      component: Swipdetail
    }

  ]
});
router.beforeEach((to,from,next)=>{
  if (to.path == "/login" || to.path == "/register" || to.path == "/code") {
    next();
  }else{
    if(sessionStorage.getItem("token")){
      next()
    }else{
      next("/login")
    }
  }
})

export default router;